package com.yanqu.road.dao.impl.activity.douluodalu;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.douluodalu.log.DouLuoDaLuTryTrainRecordData;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DouLuoDaLuTryTrainRecordDataDaoImpl extends TempDataObjectDao<DouLuoDaLuTryTrainRecordData> {

    public static String getDouLuoDaLuTryTrainRecordDataSql = "select * from `t_c_douluodalu_baoku_notice` where `activityId`=? and  `time` >?  and groupId=? order by `time` desc limit 100";

    public List<DouLuoDaLuTryTrainRecordData> getDouLuoDaLuTryTrainRecordData(long activityId, long todayTime, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<DouLuoDaLuTryTrainRecordData> result = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuTryTrainRecordDataSql);
            int i = 1;
            call.setLong(i++, activityId);
            call.setLong(i++, todayTime);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                result.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertDouLuoDaLuTryTrainRecordDataSql = "insert into `t_c_douluodalu_baoku_notice`(activityId,userId,userBaseInfo,rewardId,time) values (?,?,?,?,?)";

    @Override
    protected boolean addData(DouLuoDaLuTryTrainRecordData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRewardId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTime()));
        if (getDb().executeNoneQuery(insertDouLuoDaLuTryTrainRecordDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoDaLuTryTrainRecordDataSql = "update `t_c_douluodalu_baoku_notice` set userId=?,userBaseInfo=?,rewardId=?,time=? where activityId=?";

    @Override
    protected boolean updateData(DouLuoDaLuTryTrainRecordData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRewardId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateDouLuoDaLuTryTrainRecordDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoDaLuTryTrainRecordData parseData(ResultSet rs) throws SQLException {
        DouLuoDaLuTryTrainRecordData data = new DouLuoDaLuTryTrainRecordData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));
        data.setRewardId(rs.getInt("rewardId"));
        data.setTime(rs.getLong("time"));

        return data;
    }
}
